﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.UPMS.Component.BusinessPart;
using ZDSoft.UPMS.Model.BusinessPart;
using ZDSoft.UPMS.Service.BusinessPart;
using ZDSoft.UPMS.Core;
using NHibernate.Criterion;
using ZDSoft.UPMS.Model.SystemPart;
using ZDSoft.UPMS.Service.SystemPart;
using ZDSoft.UPMS.Component.SystemPart;
using ZDSoft.UPMS.Manager.SystemPart;

namespace ZDSoft.UPMS.Web.Controllers
{
    public class OrganizationController : Controller
    {
        //
        // GET: /Organization/
        public ActionResult Create(string id, string pid, string name)
        {
            IList<Organization> list = Container.Instance.Resolve<IOrganizationService>().FindAll();
            string str = GetZtree(list);
            TempData["ZTree"] = str;


            return View();
        }

        public void Updates(int ID, bool IsActive, string Name)
        {


            Organization Organizations = Container.Instance.Resolve<IOrganizationService>().FindByID(ID);
            Organizations.IsActive = IsActive;
            Organizations.Name = Name;
            Container.Instance.Resolve<IOrganizationService>().Update(Organizations);

        }
        public string GetZtree(IList<Organization> list)
        {
            //{ id: 1, pId: 0, name: "教师工作评价指标", url: "/test/CreateTest", target: "testIframe", open: false },
            string str = "";
            foreach (Organization org in list)
            {

                if (org.SubOrganization == null)
                {
                    str += "{id:" + org.ID + ",pId:0" + ",name:\"" + org.Name + "\",open: false,url: \"/Organization/Update?ID=" + org.ID + "\",target:\"testIframe\", open: \"false\",iconOpen:\"../../Content/css/zTreeStyle/img/diy/1_open.png\",iconClose:\"../../Content/css/zTreeStyle/img/diy/1_close.png\",icon:\"../../Content/css/zTreeStyle/img/diy/1_close.png\" },";
                }
                else
                {
                    str += "{id:" + org.ID + ",pId:" + org.SubOrganization.ID + ",name:\"" + org.Name + "\",open: false,url: \"/Organization/Update?ID=" + org.ID + "\",target:\"testIframe\", open: \"false\",iconOpen:\"../../Content/css/zTreeStyle/img/diy/1_close.png\",iconClose:\"../../Content/css/zTreeStyle/img/diy/1_close.png\",icon:\"../../Content/css/zTreeStyle/img/diy/9.png\"  },";

                }

            }
            return str;
        }
        [HttpPost]
        public ActionResult Create(Organization organ)
        {
            Container.Instance.Resolve<IOrganizationService>().Create(organ);
            return RedirectToAction("index");
        }
        public ActionResult Update(int id)
        {
            Organization Organizations = Container.Instance.Resolve<IOrganizationService>().FindByID(id);
            IList<User> users = Container.Instance.Resolve<IUserService>().FindAll();
            IList<User> Teachers = new List<User>();

            foreach (User user in users)
            {
                if (user.Organization == null)
                {
                    continue;
                }
                else if (Organizations.SubOrganization != null && Organizations.Organizations.Count == 0 && Organizations.SubOrganization.SubOrganization == null)
                {
                    if (Organizations.ID == user.Organization.ID)
                    {
                        Teachers.Add(user);
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    for (int i = 0; i < user.Roles.Count; i++)
                    {
                        if (user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && Organizations.ID == user.Organization.ID || user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && Organizations.Organizations == user.Organization || user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && user.Organization.Organizations == Organizations || user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && user.Organization.Organizations[0].Organizations == Organizations)
                        {
                            if (i >= 1)
                            {
                                continue;
                            }
                            else
                            {
                                Teachers.Add(user);
                            }
                        }
                        else if (user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && Organizations.Organizations.Count == 0 && Organizations.SubOrganization != null)
                        {
                            if (user.Organization.SubOrganization != null)
                            {


                                if (Organizations.SubOrganization.SubOrganization.ID == user.Organization.SubOrganization.ID || Organizations.SubOrganization.SubOrganization.ID == user.Organization.ID)
                                {
                                    if (i >= 1)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        Teachers.Add(user);
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                if (user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && user.Organization.ID == Organizations.SubOrganization.SubOrganization.ID)
                                {
                                    if (i >= 1)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        Teachers.Add(user);
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
            TempData["teachers"] = Teachers;
            TempData["Orname"] = Organizations.Name;
            TempData["id"] = id;
            TempData["Isactive"] = Organizations.IsActive;
            return View(Organizations);
        }
        [HttpPost]
        public string Update(string OrganlizationId, int Id)
        {
            IList<Organization> orggs = new List<Organization>();
            ZDSoft.UPMS.Model.BusinessPart.Organization org = new Organization();
            string a = Container.Instance.Resolve<IOrganizationService>().AddOrgain(OrganlizationId, Id);
            TempData["OrganlizationID"] = a;
            TempData["OrganlizationName"] = OrganlizationId;
            return "";
        }

        [HttpPost]
        public ActionResult AddOrg(string name)
        {
            Container.Instance.Resolve<IOrganizationService>().createOrg(name);
            return RedirectToAction("Create/");
        }
        public ActionResult ConfigurationOrgainazation(int id)
        {
            Organization Organizations = Container.Instance.Resolve<IOrganizationService>().FindByID(id);
            allcourse();
            IList<User> users = Container.Instance.Resolve<IUserService>().FindAll();
            IList<User> Teachers = new List<User>();
            foreach (User user in users)
            {
                int aaa = Organizations.Organizations.Count;
                if (user.Organization == null || aaa != 0)
                {
                    continue;
                }
                else
                {
                    for (int i = 0; i < user.Roles.Count; i++)
                    {

                        if (user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && Organizations.SubOrganization.ID == user.Organization.ID || user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && Organizations.SubOrganization.SubOrganization.ID == user.Organization.ID)
                        {

                            if (i >= 1)
                            {
                                continue;
                            }
                            else
                            {
                                Teachers.Add(user);
                            }
                        }
                        else if (user.Organization.SubOrganization != null)
                        {
                            if (user.Roles[i].Name != "学生" && user.Roles[i].Name != "系统管理员" && Organizations.SubOrganization.SubOrganization.ID == user.Organization.SubOrganization.ID)
                            {
                                if (i >= 1)
                                {
                                    continue;
                                }
                                else
                                {
                                    Teachers.Add(user);
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }

            TempData["teachers"] = Teachers;
            allusers();
            IList<TeacherTeachingCourse> t = new List<TeacherTeachingCourse>();

            for (int i = 0; i < Organizations.TeacherTeachingCourses.Count; i++)
            {
                TeacherTeachingCourse teaching = Container.Instance.Resolve<ITeacherTeachingCourseService>().FindByID(Organizations.TeacherTeachingCourses[i].ID);

                t.Add(teaching);
            }
            TempData["teaching"] = t;
            string str = "";
            for (int i = 0; i < t.Count; i++)
            {
                if (t[i].Teacher == null)
                {
                    continue;
                }
                else
                {
                    str += t[i].Course.ID + "," + t[i].Teacher.Name + "|";
                }
            }
            ViewBag.teacher = str;
            return View(Organizations);
        }
        [HttpPost]
        public ActionResult ConfigurationOrgainazation(Organization org, int id, string selectCoursID)
        {
            Container.Instance.Resolve<IOrganizationService>().Create(org, id, selectCoursID);
            return RedirectToAction("Configuration");
        }
        [HttpPost]
        public string Creates(string SelectedTeacherId, int id, string selectedCourseId)
        {
            Container.Instance.Resolve<IOrganizationService>().CreatTeacher(SelectedTeacherId, id, selectedCourseId);
            IList<User> teacher = new List<User>();
            string[] organlization = SelectedTeacherId.Split(',');
            foreach (string ids in organlization)
            {
                if (ids == "")
                {
                    continue;
                }
                teacher.Add(Container.Instance.Resolve<IUserService>().FindByID(int.Parse(ids)));
            }
            string strTeacherInfo = "";
            for (int i = 0; i < teacher.Count; i++)
            {
                strTeacherInfo += teacher[i].Name + ",";

            }
            strTeacherInfo = strTeacherInfo.Substring(0, strTeacherInfo.Length - 1);

            return strTeacherInfo;
        }

        [HttpPost]
        public void CoursCreate(string selectedCourseId, int id)
        {

            Container.Instance.Resolve<IOrganizationService>().Creates(selectedCourseId, id);

        }

        private void allcourse()
        {
            IList<Course> course = Container.Instance.Resolve<ICourseService>().FindAll();
            TempData["List"] = course;
        }
        private void allusers()
        {
            IList<User> users = Container.Instance.Resolve<IUserService>().FindAll();
            IList<User> Teachers = new List<User>();
            foreach (User user in users)
            {
                for (int i = 0; i < user.Roles.Count; i++)
                {
                    if (user.Roles[i].Name != "学生")
                    {
                        Teachers.Add(user);
                        break;
                    }
                }
            }
            TempData["Lists"] = Teachers;
        }

        public ActionResult principal(string SelectedTeacherName, int id)
        {
            Organization organ = Container.Instance.Resolve<IOrganizationService>().FindByID(id);
            return View(organ);
        }
        [HttpPost]
        public void principals(string SelectedTeacherName, int id)
        {
            Organization organ = Container.Instance.Resolve<IOrganizationService>().FindByID(id);
            IList<Organization> orggs = new List<Organization>();
            string[] ids = SelectedTeacherName.Split(',');
            foreach (string s in ids)
            {
                if (s == "")
                {
                    continue;
                }
                User use = Container.Instance.Resolve<IUserService>().FindByID(int.Parse(s));
                organ.ChargeMan = use;
            }
            Container.Instance.Resolve<IOrganizationService>().Update(organ);

        }
        public string FindTeacher(string q)
        {
            IList<ICriterion> conditions = new List<ICriterion>();
            if (!string.IsNullOrEmpty(q))
            {
                conditions.Add(new LikeExpression("Name", "%" + q + "%"));
            }
            IList<User> Teachers = new List<User>();

            IList<User> list = new UserManager().Find(conditions);
            foreach (User user in list)
            {
                for (int i = 0; i < user.Roles.Count; i++)
                {
                    if (user.Roles[i].Name != "学生")
                    {
                        Teachers.Add(user);

                    }
                }
            }
            string name = string.Empty;
            string id = string.Empty;
            foreach (User bill in Teachers)
            {
                name += bill.Name + "(" + bill.Organization.Name + ")" + "," + bill.ID + "\n";

            }
            if (name == "")
            {
                name += "未找到该教师" + "," + id;
            }

            return name;


        }
        OrganizationComponent organizationComponent = new OrganizationComponent();
        public String Text()
        {
            return "[{id:" + TempData["OrganlizationID"] + ",name:\"" + TempData["OrganlizationName"] + "\",open: false,url: \"/Organization/Update?ID=" + TempData["OrganlizationID"] + "\",target:\"testIframe\", open: \"false\",iconOpen:\"../../Content/css/zTreeStyle/img/diy/1_close.png\",iconClose:\"../../Content/css/zTreeStyle/img/diy/1_close.png\",icon:\"../../Content/css/zTreeStyle/img/diy/9.png\"}]";
        }
    }
}
